<template>
	<wd-tabbar fixed shape="round" v-model="tabIndex" @change="handleChanged" bordered placeholder
		custom-style="margin-bottom: 10px;">
		<wd-tabbar-item title="首页" icon="home"></wd-tabbar-item>
		<wd-tabbar-item title="下单" icon="add"></wd-tabbar-item>
		<wd-tabbar-item title="我的" icon="user" is-dot></wd-tabbar-item>
	</wd-tabbar>
</template>
<script lang="ts">
	export default {
		options: {
			addGlobalClass: true,
			virtualHost: true,
			styleIsolation: 'shared'
		}
	}
</script>
<script lang="ts" setup>
	import { onLoad } from '@dcloudio/uni-app';
	import { ref } from 'vue';
	import { getToken } from '../../store'

	const tabIndex = ref(0);
	const lastTabIndex = ref(0);

	onLoad(() => {
		const pages = getCurrentPages();
		const page = pages[pages.length - 1]

		switch (page ? page.route : '') {
			case 'pages/me/Index':
				tabIndex.value = 2
				break
			case 'pages/index/Index':
			default:
				tabIndex.value = 0
				break
		}

		lastTabIndex.value = tabIndex.value
	})

	function handleChanged({ value } : { value : number }) {
		switch (value) {
			case 0:
				uni.redirectTo({
					url: '/pages/index/Index'
				})
				break
			case 1:
				tabIndex.value = lastTabIndex.value

				uni.redirectTo({
					url: '/pages/order/Create'
				})
				break
			case 2:
				if(!getToken()) {
					uni.redirectTo({
						url: '/pages/login/Index'
					})
				} else {
					uni.redirectTo({
						url: '/pages/me/Index'
					})
				}
				break
		}

		lastTabIndex.value = tabIndex.value
	}
</script>
<style lang="scss" scoped>
</style>